"use strict";
cc._RF.push(module, '8e5b7ZoW8FHbo2+rROOT/5g', 'InstanceEvent');
// eazax-ccc/core/InstanceEvent.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InstanceEvent = void 0;
/**
 * 实例事件
 * @see InstanceEvent.ts https://gitee.com/ifaswind/eazax-ccc/blob/master/core/InstanceEvent.ts
 * @example
 * const startEvent = new InstanceEvent();
 * startEvent.on(this.onStart, this);
 * startEvent.emit();
 * startEvent.off(this.onStart, this);
 */
var InstanceEvent = /** @class */ (function () {
    function InstanceEvent() {
        this.events = null;
        this.onceEvents = null;
        this.events = new Array();
        this.onceEvents = new Array();
    }
    /**
     * 监听事件
     * @param callback 回调
     * @param target 订阅对象
     */
    InstanceEvent.prototype.on = function (callback, target) {
        this.events.push({ callback: callback, target: target });
    };
    /**
     * 监听事件（一次性）
     * @param callback 回调
     * @param target 订阅对象
     */
    InstanceEvent.prototype.once = function (callback, target) {
        this.onceEvents.push({ callback: callback, target: target });
    };
    /**
     * 取消监听事件
     * @param callback 回调
     * @param target 订阅对象
     */
    InstanceEvent.prototype.off = function (callback, target) {
        for (var i = 0; i < this.events.length; i++) {
            if (this.events[i].callback === callback && (!target || this.events[i].target === target)) {
                this.events.splice(i, 1);
            }
        }
        // 一次性事件
        for (var i = 0; i < this.onceEvents.length; i++) {
            if (this.onceEvents[i].callback === callback && (!target || this.onceEvents[i].target === target)) {
                this.onceEvents.splice(i, 1);
            }
        }
    };
    /**
     * 发射事件
     * @param args 参数
     */
    InstanceEvent.prototype.emit = function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        var promises = [];
        for (var i = 0; i < this.events.length; i++) {
            promises.push(this.events[i].callback.apply(this.events[i].target, args));
        }
        // 一次性事件
        for (var i = 0; i < this.onceEvents.length; i++) {
            promises.push(this.onceEvents[i].callback.apply(this.onceEvents[i].target, args));
        }
        this.onceEvents.length = 0;
        return Promise.all(promises);
    };
    /**
     * 取消所有监听
     */
    InstanceEvent.prototype.removeAll = function () {
        this.events.length = 0;
        this.onceEvents.length = 0;
    };
    return InstanceEvent;
}());
exports.InstanceEvent = InstanceEvent;

cc._RF.pop();